package org.ehe.business.supplier.controller;

import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.business.engineer.domain.vo.EngineerEntityVO;
import org.ehe.business.supplier.domain.bo.SupplierInfoBO;
import org.ehe.business.supplier.domain.dto.CreateSupplierRequest;
import org.ehe.business.supplier.domain.dto.SupplierQueryRequest;
import org.ehe.business.supplier.domain.dto.UpdateSupplierRequest;
import org.ehe.business.supplier.domain.vo.SupplierInfoVO;
import org.ehe.business.supplier.service.SupplierInfoService;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.common.mybatis.utils.PaginationUtils;
import org.ehe.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供应商管理
 * @author :zhangnn
 * @className :SupplierController
 * @description: 供应商信息管理
 * @date 2025-07-23 10:23:43
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/supplier")
public class SupplierController extends BaseController {

    private final SupplierInfoService supplierInfoService;

    /**
     * 供应商列表
     * @param
     * @return 分页数据
     */
    @PostMapping("/list")
    public TableDataInfo<SupplierInfoVO> getAllSuppliers(@RequestBody SupplierInfoBO engineerBO, PageQuery query) {

        return supplierInfoService.getAllSuppliers(engineerBO,query);
    }

    /**
     * 供应商详情
     * @param id 供应商ID
     * @return 供应商详细信息
     */
    @GetMapping("/{id}")
    public R<SupplierInfoVO> getSupplier(@PathVariable Long id) {
        return R.ok(supplierInfoService.getSupplierDetail(id));
    }

    /**
     * 创建供应商
     * @param request 创建请求
     * @return 操作结果
     */
    @Log(title = "新增供应商", linkType = "supplier", businessType = BusinessType.INSERT, idField = "supplierName", nameField = "supplierName")
    @PostMapping("/create")
    public R<Void> createSupplier(@RequestBody CreateSupplierRequest request) {
        request.setCreatedBy(LoginHelper.getUserId());
        request.setTenantId(LoginHelper.getTenantId());
        return toAjax(supplierInfoService.createSupplier(request));
    }

    /**
     * 修改供应商
     * @param request 修改请求
     * @return 操作结果
     */
    @Log(title = "修改供应商信息", linkType = "supplier", businessType = BusinessType.UPDATE, idField = "supplierName", nameField = "supplierName")
    @PostMapping("/update")
    public R<Void> updateSupplier(@RequestBody UpdateSupplierRequest request) {
        request.setUpdateBy(LoginHelper.getUserId().toString());
        request.setTenantId(LoginHelper.getTenantId());
        return toAjax(supplierInfoService.updateSupplier(request));
    }

    /**
     * 删除供应商
     * @param id 供应商ID
     * @return 操作结果
     */
    @Log(title = "删除供应商", linkType = "supplier", businessType = BusinessType.DELETE, idField = "supplierName")
    @DeleteMapping("/{id}")
    public R<Void> deleteSupplier(@PathVariable Long id) {
        return toAjax(supplierInfoService.deleteSupplier(id));
    }

    /**
     * 批量删除供应商
     * @param ids 供应商ID列表
     * @return 操作结果
     */
    @Log(title = "批量删除供应商", linkType = "supplier", businessType = BusinessType.DELETE)
    @DeleteMapping("/batch")
    public R<Void> batchDeleteSuppliers(@RequestBody List<Long> ids) {
        return toAjax(supplierInfoService.batchDeleteSuppliers(ids));
    }

    /**
     * 启用/停用供应商
     * @param id 供应商ID
     * @param status 状态 (0:停用 1:启用)
     * @return 操作结果
     */
    @Log(title = "修改供应商状态", linkType = "supplier", businessType = BusinessType.UPDATE, idField = "id")
    @PostMapping("/{id}/status/{status}")
    public R<Void> updateSupplierStatus(@PathVariable Long id, @PathVariable Integer status) {
        return toAjax(supplierInfoService.updateSupplierStatus(id, status));
    }

    /**
     * 获取供应商分类关联信息
     * @param id 供应商ID
     * @return 分类信息列表
     */
    @GetMapping("/{id}/categories")
    public R<List<Long>> getSupplierCategories(@PathVariable Long id) {
        return R.ok(supplierInfoService.getSupplierCategories(id));
    }

    /**
     * 更新供应商分类关联
     * @param id 供应商ID
     * @param categoryIds 分类ID列表
     * @return 操作结果
     */
    @Log(title = "更新供应商分类", linkType = "supplier", businessType = BusinessType.UPDATE, idField = "id")
    @PostMapping("/{id}/categories")
    public R<Void> updateSupplierCategories(@PathVariable Long id, @RequestBody List<Long> categoryIds) {
        return toAjax(supplierInfoService.updateSupplierCategories(id, categoryIds));
    }

    /**
     * 导出供应商信息
     */
    @PostMapping("/export")
    @Log(title = "导出供应商信息", businessType = BusinessType.EXPORT)
    public void export(SupplierInfoBO bo, HttpServletResponse response) {
        log.info("导出供应商信息: {}", bo);

        try {
            // 查询所有符合条件的供应商
            PageQuery pageQuery = new PageQuery(1,Integer.MAX_VALUE);
            TableDataInfo<SupplierInfoVO> dataInfo = supplierInfoService.getAllSuppliers(bo, pageQuery);
            List<SupplierInfoVO> list = dataInfo.getRows();

            ExcelUtil.exportExcel(list, "供应商信息", SupplierInfoVO.class, response);
        } catch (Exception e) {
            log.error("导出供应商信息失败", e);
            throw new RuntimeException("导出失败: " + e.getMessage());
        }
    }

}
